package controllers

import (
	"LoveU/models"
	"LoveU/utils"
	"fmt"
	"strconv"

	"github.com/astaxie/beego"
)

const HOST = "http://120.24.168.102:8080/static/admin/"

type AdminController struct {
	beego.Controller
}

// 登录
func (this *AdminController) AdminLogin() {
	name := this.Input().Get("name")
	pwd := this.Input().Get("pwd")
	admin := models.GetAdmin(name, pwd)
	var codejson interface{}
	if admin != nil {
		adminSession, _ := globalSessions.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
		codejson = utils.Message{utils.LOGIN_OK, "success", nil, adminSession.SessionID()}
		SessionStoreAdmin[adminSession.SessionID()] = adminSession
		adminSession.Set("name", name)
		adminSession.Set("pwd", pwd)
		adminSession.Set("adminid", admin.Id)
	} else {
		codejson = utils.Message{utils.USER_NOT_FOUND, "用户不存在", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

// 查阅VIP申请
// 返回VIP申请的List
func (this *AdminController) VipRequestList() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	adminid := mySession.Get("adminid")
	if adminid == nil {
		beego.Error(48)
		notLoginAdmin(this)
		return
	}
	page_str := this.Input().Get("page")
	if page_str == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误41", "", nil)
		return
	}
	page, err0 := strconv.ParseInt(page_str, 10, 64)
	pageSize_str := this.Input().Get("pagesize")
	if pageSize_str == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误47", "", nil)
		return
	}
	pageSize, err1 := strconv.ParseInt(pageSize_str, 10, 64)

	if err0 != nil || err1 != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误63", "", nil)
		return
	}
	requests, status := models.GetVIPList(page, pageSize)
	if status == utils.UNKNOWN_ERROR {
		getJSONAdmin(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONAdmin(this, utils.OK, "success", "", requests)
	return
}

// 返回某用户的VIP申请资料
func (this *AdminController) GetVipRequest() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	adminid := mySession.Get("adminid")
	if adminid == nil {
		beego.Error(48)
		notLoginAdmin(this)
		return
	}
	useridStr := this.Input().Get("userid")
	userid, err := strconv.ParseInt(useridStr, 10, 64)
	if err != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
		return
	}
	request := models.GetVipRequest(userid)
	if request == nil {
		getJSONAdmin(this, utils.USER_REALINFO_NOT_FOUND, "未找到申请记录，或数据库错误", "", nil)
		return
	}
	getJSONAdmin(this, utils.OK, "success", "", request)
	return
}

// 获取用户信息
func (this *AdminController) GetUserInfo() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	// adminid := mySession.Get("adminid")
	useridStr := this.Input().Get("userid")
	userid, err := strconv.ParseInt(useridStr, 10, 64)
	if err != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
		return
	}
	user := models.GetUser2(userid)
	if user == nil {
		getJSONAdmin(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONAdmin(this, utils.OK, "success", "", user)
	return
}

// 同意用户的VIP申请
func (this *AdminController) AgreeRequest() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	// adminid := mySession.Get("adminid")
	idStr := this.Input().Get("id")
	agreeStr := this.Input().Get("agree")
	reason := this.Input().Get("reason")
	pic1 := this.Input().Get("url1")
	pic2 := this.Input().Get("url2")
	pic3 := this.Input().Get("url3")

	id, err0 := strconv.ParseInt(idStr, 10, 64)
	agree, err1 := strconv.ParseInt(agreeStr, 10, 64)
	if err0 != nil || err1 != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误63", "", nil)
		return
	}
	status := models.AgreeVipRequest(id, agree, reason, pic1, pic2, pic3)
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", nil)
		return
	}
	getJSONAdmin(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
	return

}

//获取活动圈信息
func (this *AdminController) GetActivities() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}

	page_str := this.Input().Get("page")
	if page_str == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误41", "", nil)
		return
	}
	page, err0 := strconv.ParseInt(page_str, 10, 64)
	pageSize_str := this.Input().Get("pagesize")
	if pageSize_str == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误47", "", nil)
		return
	}
	pageSize, err1 := strconv.ParseInt(pageSize_str, 10, 64)

	if err0 != nil || err1 != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误63", "", nil)
		return
	}
	activities, status := models.GetActivities(page, pageSize)
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", activities)
		return
	}
	getJSONAdmin(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
	return

}

// 发布活动
func (this *AdminController) PublishActivity() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	adminid := mySession.Get("adminid")
	content := this.Input().Get("content")
	status := models.PublishActivity(content, adminid.(int64))
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", nil)
		return
	}
	getJSONAdmin(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
	return
}

// 删除活动
func (this *AdminController) DeleteActivity() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	adminid := mySession.Get("adminid")
	idStr := this.Input().Get("id")
	id, err0 := strconv.ParseInt(idStr, 10, 64)
	if err0 != nil {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
	}
	status := models.DeleteActivity(adminid.(int64), id)
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", nil)
		return
	}
	getJSONAdmin(this, utils.UNKNOWN_ERROR, "参数格式错误", "", nil)
	return
}

// 关闭用户账号
func (this *AdminController) DeleteUser() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	// adminid := mySession.Get("adminid")
	name := this.Input().Get("name")
	if name == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数格式错误", "", nil)
		return
	}
	status := models.DeleteUser(name)
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", nil)
		return
	}
	getJSONAdmin(this, utils.USER_NOT_FOUND, "用户不存在", "", nil)
	return
}

// 冻结用户账号
func (this *AdminController) FrozenUser() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	// adminid := mySession.Get("adminid")
	name := this.Input().Get("name")
	if name == "" {
		getJSONAdmin(this, utils.ARGUMENT_DENIED, "参数格式错误215", "", nil)
		return
	}
	status := models.FrozenUser(name)
	beego.Error(status)
	if status == utils.OK {
		getJSONAdmin(this, utils.OK, "success", "", nil)
		return
	}
	getJSONAdmin(this, utils.USER_NOT_FOUND, "用户不存在", "", nil)
	return
}

const ADMINPATH = "static/admin/"

//上传文件
func (this *AdminController) UploadPhoto() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}

	f, h, err := this.GetFile("photo")
	if err != nil {
		fmt.Println("getfile err ", err)
	} else {
		filePath := ADMINPATH + h.Filename
		this.SaveToFile("photo", filePath) // 保存位置在 static/admin, 没有文件夹要先创建
		f.Close()

		// 修改用户上传文件的名字
		// 先建立相应的文件夹
		filetype, filename := picTypeAll(filePath, ADMINPATH)
		var codejson interface{}

		switch filetype {
		case utils.UNKNOWN_ERROR:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		case utils.ALBUM_DELETE_FAILED:
			codejson = utils.Message{utils.ALBUM_DELETE_FAILED, "删除文件失败", nil, ""}
		case utils.ALBUM_RENAME_FAILED:
			codejson = utils.Message{utils.ALBUM_RENAME_FAILED, "重命名失败", nil, ""}
		case utils.ALBUM_UPLOAD_JPEG:

			codejson = utils.Message{utils.ALBUM_UPLOAD_JPEG, "success", HOST + filename, ""}
		case utils.ALBUM_UPLOAD_GIF:
			codejson = utils.Message{utils.ALBUM_UPLOAD_GIF, "success", HOST + filename, ""}
		case utils.ALBUM_UPLOAD_PNG:
			codejson = utils.Message{utils.ALBUM_UPLOAD_PNG, "success", HOST + filename, ""}
		case utils.ALBUM_UPLOAD_FORMAT_DENIED:
			codejson = utils.Message{utils.ALBUM_UPLOAD_FORMAT_DENIED, "不兼容的图片类型", nil, ""}
		default:
			codejson = utils.Message{utils.UNKNOWN_ERROR, "系统错误", nil, ""}
		}
		this.Data["json"] = codejson
		this.ServeJSON()
	}
}

func (this *AdminController) GetActivityInfo() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStoreAdmin[sid]
	if mySession == nil {
		notLoginAdmin(this)
		return
	}
	// adminid := mySession.Get("adminid")
}
